<?php
class ReportController extends Controller
{
	public function actionSite()
	{
		$transaction = Yii::app()->db->begintransaction();
		try {
			if(!isset($_GET['type'], $_GET['uid'], $_GET['place_id'])) {
				throw new PlatwoException(1100, 'Needs property type, uid, place_id');
			}
			if(!Helper::checkUser($_GET['uid'])){
				throw new PlatwoException(20);
			}
			$report = new Report();
			$report['type'] = $_GET['type'];
			$report['uid'] = $_GET['uid'];
			if(!$report->save()){
				throw new PlatwoException(2000);
			}
			if($_GET['type'] == 'SITE_INDECENT' OR $_GET['type'] == 'SITE_FALSE' OR $_GET['type'] == 'SITE_HARRY'){
				if(!Helper::checkSite($_GET['place_id'])){
					throw new PlatwoException(35);
				}
				$reportSite = new ReportSite;
				$reportSite->id = $report->id;
				$reportSite->place_id = $_GET['place_id'];
				if(!$reportSite->save()){
					throw new PlatwoException(2001);
				}
			}
			else if($_GET['type'] == 'SITE_TEXT'){
				if(!isset($_GET['text'])) {
					throw new PlatwoException(9999, "need parameter text");
				}
				if(!Helper::checkSite($_GET['place_id'])){
					throw new PlatwoException(35);
				}
				$reportText = new ReportText;
				$reportText->id = $report->id;
				$reportText->text = $_GET['text'];
				if(!$reportText->save()){
					throw new PlatwoException(2002);
				}
			}
			else {
				throw new PlatwoException(2003);
			}
			$transaction->commit();
			$e = new PlatwoException(0);
			echo $e->toJSON();
		} catch(PlatwoException $e) {
			$transaction->rollback();
			echo $e->toJSON();
		}
	}
	
	public function actionPhoto()
	{
		$transaction = Yii::app()->db->begintransaction();
		$typeList = array('PHOTO_INDECENT', 'PHOTO_FALSE', 'PHOTO_HARRY', 'PHOTO_TEXT');
		try {
			if(!isset($_GET['type'], $_GET['uid'], $_GET['photo_id'])) {
				throw new PlatwoException(1100, 'Needs parameters type, uid, photo_id');
			}
			if(array_search($_GET['type'], $typeList) === FALSE) {
				throw new PlatwoException(9999, 'type '.$_GET['type'].' Invalid');
			}
			if(Photo::model()->findByPk($_GET['photo_id']) == null) {
				throw new PlatwoException(9999, 'not found photo.');
			}
			$report = new Report();
			$report->type = $_GET['type'];
			$report->uid = $_GET['uid'];
			if(!$report->save()) {
				throw new PlatwoException(9999, 'report save failed.');
			}
			$reportPhoto = new ReportPhoto();
			$reportPhoto->id = $report->id;
			$reportPhoto->photo_id = $_GET['photo_id'];
			if(!$reportPhoto->save()) {
				throw new PlatwoException(9999, 'reportPhoto save failed.');
			}
			if($_GET['type'] == 'PHOTO_TEXT') {
				if(!isset($_GET['text'])) {
					throw new PlatwoException(1100, 'Needs parameter text');
				}
				$reportText = new ReportText();
				$reportText->id = $report->id;
				$reportText->text = $_GET['text'];
				if(!$reportText->save()) {
					throw new PlatwoException(9999, 'reportText save failed.');
				}
			}
			$e = new PlatwoException(0);
			echo $e->toJSON();
			$transaction->commit();
		} catch(PlatwoException $e) {
			$transaction->rollback();
			echo $e->toJSON();
		}
	}
	
	public function actionComment()
	{
		$transaction = Yii::app()->db->begintransaction();
		$typeList = array('COMMENT_INDECENT', 'COMMENT_FALSE', 'COMMENT_HARRY', 'COMMENT_TEXT');
		try {
			if(!isset($_GET['type'], $_GET['uid'], $_GET['comment_id'])) {
				throw new PlatwoException(1100, 'Needs parameters type, uid, comment_id');
			}
			if(array_search($_GET['type'], $typeList) === FALSE) {
				throw new PlatwoException(9999, 'type '.$_GET['type'].' Invalid');
			}
			if(Comment::model()->findByPk($_GET['comment_id']) == null) {
				throw new PlatwoException(9999, 'not found comment.');
			}
			$report = new Report();
			$report->type = $_GET['type'];
			$report->uid = $_GET['uid'];
			if(!$report->save()) {
				throw new PlatwoException(9999, 'report save failed.');
			}
			$reportComment = new ReportComment();
			$reportComment->id = $report->id;
			$reportComment->comment_id = $_GET['comment_id'];
			if(!$reportComment->save()) {
				throw new PlatwoException(9999, 'reportComment save failed.');
			}
			if($_GET['type'] == 'COMMENT_TEXT') {
				if(!isset($_GET['text'])) {
					throw new PlatwoException(1100, 'Needs parameter text');
				}
				$reportText = new ReportText();
				$reportText->id = $report->id;
				$reportText->text = $_GET['text'];
				if(!$reportText->save()) {
					throw new PlatwoException(9999, 'reportText save failed.');
				}
			}
			$e = new PlatwoException(0);
			echo $e->toJSON();
			$transaction->commit();
		} catch(PlatwoException $e) {
			$transaction->rollback();
			echo $e->toJSON();
		}
	}
	
	/******************* internal function *****************/
	
	protected function addText($id, $text)
	{
		
	}
}